package com.smedi.ismedi.personnel.core.port.adapter.service.impl;

import com.smedi.ismedi.authoritymanagement.api.dto.request.ChangeMobileRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.request.CreateAccountBasicRequest;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAccountManagementFeignApi;
import com.smedi.ismedi.personnel.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.personnel.core.port.adapter.service.UserManagementRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: xulinjun
 * @Date: 2021-7-2 17:40
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserManagementRemoteApiServiceImpl implements UserManagementRemoteApiService {
    private final RemoteAccountManagementFeignApi remoteAccountManagementFeignApi;

    @Override
    public String generateAccount(Long personId, String mobile, String idCard, String lastName) {
        CreateAccountBasicRequest createAccountBasicRequest = new CreateAccountBasicRequest()
                .setIdCard(idCard)
                .setMobile(mobile)
                .setLastName(lastName)
                .setPersonId(personId);
        ResponseData<String> response = remoteAccountManagementFeignApi.postCreateAccount(createAccountBasicRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public void enableAccount(Long personId) {
        ResponseData<Boolean> response = remoteAccountManagementFeignApi.putEnable(personId);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public void disableAccount(Long personId) {
        ResponseData<Boolean> response = remoteAccountManagementFeignApi.putDisable(personId);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public void deleteAccount(Long personId) {
        ResponseData<Boolean> response = remoteAccountManagementFeignApi.deleteAccount(personId);
        if (!response.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public void deleteAccountOnly(Long personId) {
        ResponseData<Boolean> response = remoteAccountManagementFeignApi.deleteAccountOnly(personId);
        if (!response.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public void changeAccountMobile(Long personId, String mobile) {
        ChangeMobileRequest changeMobileRequest = new ChangeMobileRequest()
                .setMobile(mobile)
                .setPersonId(personId);
        ResponseData<Boolean> response = remoteAccountManagementFeignApi.postChangeMobile(changeMobileRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }
}
